/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.devmng.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.devmng.entity.DeviceType;
import com.thinkgem.jeesite.modules.devmng.service.DeviceTypeService;

/**
 * 类型Controller
 * @author panda
 * @version 2017-12-02
 */
@Controller
@RequestMapping(value = "${adminPath}/devmng/deviceType")
public class DeviceTypeController extends BaseController {

	@Autowired
	private DeviceTypeService deviceTypeService;
	
	@ModelAttribute
	public DeviceType get(@RequestParam(required=false) String id) {
		DeviceType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = deviceTypeService.get(id);
		}
		if (entity == null){
			entity = new DeviceType();
		}
		return entity;
	}
	
	@RequiresPermissions("devmng:deviceType:view")
	@RequestMapping(value = {"list", ""})
	public String list(DeviceType deviceType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<DeviceType> list = deviceTypeService.findList(deviceType); 
		model.addAttribute("list", list);
		return "modules/devmng/deviceTypeList";
	}

	@RequiresPermissions("devmng:deviceType:view")
	@RequestMapping(value = "form")
	public String form(DeviceType deviceType, Model model) {
		if (deviceType.getParent()!=null && StringUtils.isNotBlank(deviceType.getParent().getId())){
			deviceType.setParent(deviceTypeService.get(deviceType.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(deviceType.getId())){
				DeviceType deviceTypeChild = new DeviceType();
				deviceTypeChild.setParent(new DeviceType(deviceType.getParent().getId()));
				List<DeviceType> list = deviceTypeService.findList(deviceType); 
				if (list.size() > 0){
					deviceType.setSort(list.get(list.size()-1).getSort());
					if (deviceType.getSort() != null){
						deviceType.setSort(deviceType.getSort() + 30);
					}
				}
			}
		}
		if (deviceType.getSort() == null){
			deviceType.setSort(30);
		}
		model.addAttribute("deviceType", deviceType);
		return "modules/devmng/deviceTypeForm";
	}

	@RequiresPermissions("devmng:deviceType:edit")
	@RequestMapping(value = "save")
	public String save(DeviceType deviceType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deviceType)){
			return form(deviceType, model);
		}
		deviceTypeService.save(deviceType);
		addMessage(redirectAttributes, "保存类型成功");
		return "redirect:"+Global.getAdminPath()+"/devmng/deviceType/?repage";
	}
	
	@RequiresPermissions("devmng:deviceType:edit")
	@RequestMapping(value = "delete")
	public String delete(DeviceType deviceType, RedirectAttributes redirectAttributes) {
		deviceTypeService.delete(deviceType);
		addMessage(redirectAttributes, "删除类型成功");
		return "redirect:"+Global.getAdminPath()+"/devmng/deviceType/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<DeviceType> list = deviceTypeService.findList(new DeviceType());
		for (int i=0; i<list.size(); i++){
			DeviceType e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}